package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.powernode.entity.UmsRoleUser;
import com.powernode.mapper.UmsRoleUserMapper;
import com.powernode.service.UmsRoleUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户与角色关系表 服务实现类
 * </p>
 *
 * @author lxl
 * @since 2022-04-09
 */
@Service
public class UmsRoleUserServiceImpl extends ServiceImpl<UmsRoleUserMapper, UmsRoleUser> implements UmsRoleUserService {

    @Transactional
    public Boolean save(Long roleId, Long[] userIds) {
        //先删除数据库中的数据
        UpdateWrapper<UmsRoleUser> wrapper = new UpdateWrapper<UmsRoleUser>();
        wrapper.eq("role_id",roleId);
        this.remove(wrapper);

        List<UmsRoleUser> list = new ArrayList<UmsRoleUser>();
        if (userIds != null){
            for (Long userId : userIds) {
                list.add(new UmsRoleUser(roleId,userId));
            }
        }
        return this.saveBatch(list);
    }

    public List<UmsRoleUser> getByRoleId(Long roleId) {
        QueryWrapper<UmsRoleUser> wrapper = new QueryWrapper<UmsRoleUser>();
        wrapper.eq("role_id",roleId);
        return this.list(wrapper);
    }
}
